Skip to content

Restructure Python custom operators guide#3916

Open
zou3519 wants to merge 1 commit into
mainfrom
custom-python-ops-guide-refactor
Open

Restructure Python custom operators guide#3916
zou3519 wants to merge 1 commit into
mainfrom
custom-python-ops-guide-refactor

Conversation

@zou3519
Copy link
Copy Markdown
Contributor

@zou3519 zou3519 commented Jun 3, 2026

Split the custom Python operators tutorial into an overview plus focused pages for functional operators, mutable operators, and optional registrations. Emphasize required schema and mutation/aliasing contracts, opcheck validation, fake kernels for torch.compile/export, and 2.13-only in-place/out custom operator behavior.

Add the new pages under the custom operators landing page so the left navigation exposes a nested custom-ops section and users can move through the guide in order.

Validated with lintrunner -m main and make clean-cache && make html-noplot.

This was generated by codex and I didn't read it carefully (but I did read most of it).

Split the custom Python operators tutorial into an overview plus focused pages for functional operators, mutable operators, and optional registrations. Emphasize required schema and mutation/aliasing contracts, opcheck validation, fake kernels for torch.compile/export, and 2.13-only in-place/out custom operator behavior.

Add the new pages under the custom operators landing page so the left navigation exposes a nested custom-ops section and users can move through the guide in order.

Validated with lintrunner -m main and make clean-cache && make html-noplot.
@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented Jun 3, 2026

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/tutorials/3916

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit ebf88d6 with merge base 326edb0 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the cla signed label Jun 3, 2026
@zou3519
Copy link
Copy Markdown
Contributor Author

zou3519 commented Jun 4, 2026

@zou3519 zou3519 requested review from angelayi and frgossen June 4, 2026 16:13
@zou3519 zou3519 marked this pull request as ready for review June 4, 2026 16:13
Comment on lines +6 to +7
Functional Python Custom Operators for torch.compile
====================================================
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove "for torch.compile"

Comment on lines +40 to +41
Choose one path:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This table is a little squashed, can we improve it?

Image

Decide the schema and mutation/aliasing contract before writing registrations.
PyTorch uses the schema and registrations to reason about aliasing; it does not
infer the contract from the Python body.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should define what aliasing is in PyTorch.

@zou3519
Copy link
Copy Markdown
Contributor Author

zou3519 commented Jun 4, 2026

full disclosure I didn't go through the PR in detail and am doing that in the background, but if you folks can review this at the same time I can get my agent to fix all the comments at once

input tensor, a view of an input, or two outputs that alias each other.
* A mutable custom operator must list every mutated argument in ``mutates_args``.
* A fake kernel must return tensors with the same metadata as the real kernel:
shape, dtype, device, layout, strides, and storage offset when relevant.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an example of strides? We should provide a code example of a bad example and what the fix is

Mutable Python Custom Operators
===============================

The functional page wrapped ``numpy.sin`` as an operator that returns a fresh
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"the functional page" is a bit weird to read, link to it or something

# needs this contract for functionalization, fake tensors, ``torch.compile``,
# and autograd.
#
# If the operator does not mutate a Tensor input, use the functional operator
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the operator does not mutate any Tensor input,

Comment on lines +39 to +41
# If the operator mutates keyword-only ``out=`` Tensor arguments and returns
# them, use a tagged ``out=`` operator, starting in PyTorch 2.13. Do not read
# from the ``out=`` tensors.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

combine this into one sentence somehow. Part of the definition of out= operator means that the out= tensor is write-only (no reads)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant